IDCard = {}
local setItem_hooks, cleared_hooks, buttons = {},{},{}

local function setitem(self)
    local b = buttons[self]
    local _,id = self:GetItem()
    if not id then return end
    local tex = select(10,GetItemInfo(id))
    b:SetNormalTexture(tex)
    b.id = id
    local orig = setItem_hooks[self]
    if orig then orig(self) end
end

local function cleared(self)
    buttons[self]:SetNormalTexture(nil)
    local orig = cleared_hooks[self]
    if orig then orig(self) end
end

local function click(self)
    if IsShiftKeyDown() and ChatFrameEditBox:IsVisible() and self.id then
        ChatFrameEditBox:Insert(self.id)
    elseif IsControlKeyDown() and self.id then
        DressUpItemLink(self.id)
    end
end

local function dragstart(self)self:GetParent():StartMoving()end
local function dragstop(self)self:GetParent():StopMovingOrSizing()end

function IDCard:RegisterTooltip(tooltip)
    local b = CreateFrame("Button",nil,tooltip)
    b:SetWidth(37)
    b:SetHeight(37)
    b:SetPoint("TOPRIGHT",tooltip,"TOPLEFT",0,-3)
    b:SetScript("OnDragStart",dragstart)
    b:SetScript("OnDragStop",dragstop)
    b:SetScript("OnClick",click)
    b:RegisterForDrag("LeftButton")
    buttons[tooltip] = b

    setItem_hooks[tooltip] = tooltip:GetScript("OnTooltipSetItem")
    cleared_hooks[tooltip] = tooltip:GetScript("OnTooltipCleared")
    tooltip:SetScript("OnTooltipSetItem",setitem)
    tooltip:SetScript("OnTooltipCleared",cleared)
end

IDCard:RegisterTooltip(ItemRefTooltip)